package com.hziee.graduate.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hziee.graduate.ticket.dao.CinemaMovieDao;
import com.hziee.graduate.ticket.dao.MovieDao;
import com.hziee.graduate.ticket.dto.R;
import com.hziee.graduate.ticket.entity.CinemaMovie;
import com.hziee.graduate.ticket.entity.Movie;
import com.hziee.graduate.ticket.service.CinemaMovieService;
import com.hziee.graduate.ticket.service.MovieService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Service("cinemaMovieService")
public class CinemaMovieServiceImpl extends ServiceImpl<CinemaMovieDao, CinemaMovie> implements CinemaMovieService {
    @Resource
    private MovieDao movieDao;
    @Resource
    private CinemaMovieDao cinemaMovieDao;

    @Override
    @Transactional
    public R<List<Movie>> getAllMovieByCinema(Long cinemaId) {
        List<CinemaMovie> list = query().eq("cinema_id", cinemaId).eq("del_flag", 0).list();
        ArrayList<Movie> movies = new ArrayList<>();
        for (CinemaMovie cinemaMovie : list) {
            movies.add(movieDao.selectById(cinemaMovie.getMovieId()));
        }
        return R.success(movies);
    }

    @Override
    public R<List<Movie>> getUnJoinMovieByCinema(Long cinemaId) {
        List<Movie> unJoinMovieByCinema = movieDao.getUnJoinMovieByCinema(cinemaId);
        return R.success(unJoinMovieByCinema);
    }

    @Override
    public R<Object> addCinemaMovie(Long cinemaId, Long movieId, Long userId) {
        // 从系统影库中找到这部电影,加入本地影库
        Movie movie = movieDao.selectById(movieId);
        // 插入一条影院电影记录
        CinemaMovie cinemaMovie = new CinemaMovie();
        cinemaMovie.setCinemaId(cinemaId);
        cinemaMovie.setMovieId(movieId);
        cinemaMovie.setCreatePerson(userId);
        cinemaMovie.setCreateTime(LocalDateTime.now());
        cinemaMovie.setDelFlag(0);
        int insert = cinemaMovieDao.insert(cinemaMovie);
        return R.success();
    }

    @Override
    public R<Object> delCinemaMovie(Long cinemaId, Long movieId) {
        // 查出该电影
        CinemaMovie cinemaMovie = cinemaMovieDao.selectOne(
                new QueryWrapper<CinemaMovie>()
                .eq("movie_id",movieId)
                .eq("cinema_id",cinemaId)
                .eq("del_flag",0)
        );
        // 将del_flag置为1
        cinemaMovie.setDelFlag(1);
        int update = cinemaMovieDao.update(cinemaMovie, new UpdateWrapper<CinemaMovie>()
                .eq("movie_id", movieId)
                .eq("cinema_id", cinemaId)
                .eq("del_flag",0)
        );
        return R.success();
    }

}

